package com.shujia.wyh;


import com.shujia.wyh.utils.MySqlTool;
import redis.clients.jedis.Jedis;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
import java.util.Set;

/*
    redis充当二级索引的场景
 */
public class JedisDemo3 {
    public static void main(String[] args) throws Exception {
//        createIndexInRedis();
        Jedis jedis = new Jedis("192.168.254.100", 7000);
        Scanner sc = new Scanner(System.in);
        System.out.println("您要查询商务本还是游戏本：（1 商务本，2 游戏本）");
        int i = sc.nextInt();
        sc.nextLine();
        System.out.println("请输入您要查询的关键词:");
        String next = sc.nextLine();
        String FileName = next + ".csv";
        BufferedWriter bw = new BufferedWriter(new FileWriter("redis/src/main/java/com/shujia/wyh/redis2mysql/" + FileName));
        bw.write("商品编号,名称,价格,店铺,评论数");
        bw.newLine();
        bw.flush();

        if(i==1){
            Set<String> ids = jedis.smembers("商务本:" + next);
            //拿着redis中查询到的索引，去mysql中查询
            for (String id : ids) {
                Statement statement = MySqlTool.getStatement();
                ResultSet resultSet = statement.executeQuery("select * from jd_goods where id=" + id);
                resultSet.next();
                String goods_name = resultSet.getString(2).replace("\n","");
                String goods_price = resultSet.getString(3).replace("\n","");
                String goods_shop = resultSet.getString(4).replace("\n","");
                String goods_comments = resultSet.getString(5).replace("\n","");
                String info = id+","+goods_name+","+goods_price+","+goods_shop+","+goods_comments;
                System.out.println(info);
                bw.write(info);
                bw.newLine();
                bw.flush();
            }

            System.out.println("数据已经保存至："+FileName+" 文件中, 请注意查看！");
        }else {
            Set<String> ids = jedis.smembers("游戏本:" + next);
            //拿着redis中查询到的索引，去mysql中查询
            for (String id : ids) {
                Statement statement = MySqlTool.getStatement();
                ResultSet resultSet = statement.executeQuery("select * from jd_goods where id=" + id);
                resultSet.next();
                String goods_name = resultSet.getString(2).replace("\n","");
                String goods_price = resultSet.getString(3).replace("\n","");
                String goods_shop = resultSet.getString(4).replace("\n","");
                String goods_comments = resultSet.getString(5).replace("\n","");
                String info = id+","+goods_name+","+goods_price+","+goods_shop+","+goods_comments;
                bw.write(info);
                bw.newLine();
                bw.flush();
            }
            System.out.println("数据已经保存至："+FileName+" 文件中, 请注意查看！");
        }

    }

    public static void createIndexInRedis() throws Exception{
        Scanner sc = new Scanner(System.in);
        Jedis jedis = new Jedis("192.168.254.100", 7000);

        //游戏本：机械革命  联想    商务本：Apple  荣耀
        //提前将游戏本机械革命、联想   商务本：Apple  荣耀获取id列表存储到redis中充当二级索引
        String sql = "select id from jd_goods where goods_name like ?";
        PreparedStatement statement = MySqlTool.getStatement(sql);

        while (true){
            System.out.println("请输入您要查询索引的关键词：");
            String next = sc.next();
            if("over".equals(next)){
                break;
            }

            System.out.println("游戏本 or 商务本：（1. 游戏本  2. 商务本）");
            int i = sc.nextInt();
            System.out.println(i);
            if(i==1){
                System.out.println("----------------------------");
                statement.setString(1,"%"+next+"%");
                ResultSet resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    String id = resultSet.getString(1);
                    jedis.sadd("游戏本:"+next,id);
                }
            }else {
                statement.setString(1,"%"+next+"%");
                ResultSet resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    String id = resultSet.getString(1);
                    jedis.sadd("商务本:"+next,id);
                }
            }
        }

        jedis.close();
    }
}
